import React from "react";
import styles from './index.less';
import queryString from 'query-string';
import { Modal, InputItem, Pagination, Icon, List, Radio, Toast } from 'antd-mobile';
import ReactDOM from 'react-dom';
import { apiMaps } from '@/utils/maps';
import UserServices from '@/services/user';
import { requestSuccess, addHidden, removeHidden } from '@/utils/tools';
import {
  exitWebView,
  setLeftBtn
} from '@/utils/cordova';
const RadioItem = Radio.RadioItem;
// 直接返回值

class scModal extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      visible: true,
      state: props.state,
      message: props.message,
      content: props.content,
      success: props.success,
      cancel: props.cancel,
      cities: [],
      keyword: props.enKeyword || '',
      activeCity: ''
    };

    window.cordovaBackEvents.push(() => {
      this.handleCancel();
    });
  }

  handleSuccess = (e) => {
    if(!this.state.activeCity){
      Toast.info('Please select city');
      return;
    }
    removeHidden();
    this.setState({
      visible: false,
    });
    this.state.success && this.state.success(this.state.activeCity);
  }

  handleCancel = (e) => {
    removeHidden();
    this.setState({
      visible: false,
    });
    this.state.cancel && this.state.cancel();
  }

  handleSelected = (value) => {
    this.setState({
      activeCity: value
    })
  }

  handleKeywordChange = (value) => {
    this.setState({
      keyword: value
    })
  }

  handleGetCitys = () => {
    UserServices.queryCity({
      keyWord: this.state.keyword,
      cityId: ''
    }).then(res => {
      console.log(res);
      if(requestSuccess(res)){
        const result = res.data.cityInfos;
        this.setState({
          cities: result
        })
      }
    })
  }

  componentDidMount(){
    addHidden();
  }

  render() {
    return (
      <Modal
        visible={this.state.visible}
        className= {styles.scModal}
      >
      <div className={styles.modalWrapper}>
        <div className={styles.info}>City Search Help</div>
          <div className={styles.list}>
            <div className={styles.item}>
              <div className={styles.des}>City</div>
              <div className={styles.value}>
                <InputItem
                  clear
                  placeholder="click input"
                  value={this.state.keyword}
                  onChange={this.handleKeywordChange}
                ></InputItem>
              </div>
            </div>
            <div className={styles.searchBtn} onClick={this.handleGetCitys}>Search</div>
          </div>
          <div className={styles.results}>
            {
              this.state.cities.length > 0 ?
              <div>
              <div className={styles.labels}>
                <div className={styles.label}>Chinese Name</div>
                <div className={styles.label}>English Name</div>
                <div className={styles.selected}>Selected</div>
              </div>
              <List>
                {
                  this.state.cities.map(i => (
                    <RadioItem key={i.id} checked={this.state.activeCity.id === i.id} onChange={() => this.handleSelected(i)}>
                      <div className={styles.item}>
                        <div className={styles.des}>{i.cityNameCn}</div>
                        <div className={styles.des}>{i.cityNameEn}</div>
                      </div>
                    </RadioItem>
                  ))
                }
              </List>
            </div>
              :
              null
            }
          </div>
        <div className={styles.footerBtns}>
          <div onClick={this.handleSuccess}>
            {
              this.props.state == 'edit'
              ?
              "Save"
              :
              "Add"
            }
          </div>
          <div onClick={this.handleCancel}>Close</div>
        </div>
      </div>
      </Modal>
    );
  }
}

export default {
  open(params){
    let div = document.createElement('div');
    div.id = 'searchCityModal';
    document.body.appendChild(div);
    // window.cordovaBackEvents = function(){
    //   const a = document.getElementById('searchCityModal');
    //   ReactDOM.unmountComponentAtNode(a);
    //   document.body.removeChild(a);
    //   window.cordovaBackEvents = {}
    // }
    ReactDOM.render(React.createElement(scModal, params), div);
  },
  close(){
    const a = document.getElementById('searchCityModal');
    ReactDOM.unmountComponentAtNode(a);
  }
};
